define(['module/md-left-menu/script', 'routerPath' , 'ajaxExt' , 'appCtrl'] ,
	 function( menu , urlConfig , ajax  , appCtrl ){
	window.app = angular.module('app' , ['ui.router'] ) ;
	//常量定义
	app.constant('$ctx' , {
		api 	: 	'http://172.31.24.196:9191/system/' ,
		//api 	: 	'http://127.0.0.1:9191/system/' ,
		cfgApi 	: 	'http://127.0.0.1:9393/config/' ,
		debug 	: 	true
	} ) ;
	//初始化菜单
	menu( app ) ;
	//重写Ajax服务
	ajax( app ) ;
	//初始化应用
	appCtrl( app ) ;

	//alert组件
	app.directive('ngModalAlert' , function(){
		return {
			restrict 	: 	'E' ,
			replace 	: 	true ,
			scope 		: 	true ,
			templateUrl : 	'assets/modules/ng-modal-alert/template.html' ,
			controller 	: 	function( $scope , $timeout , $element ){
				//angularjs 手动赋值需要调用$scope.$apply方法
				var def = {
					title 	: 	'标题' ,
					msg 	: 	'' ,
					btnLabel: 	'确定'
				}

				//覆盖windows alert
				window.alert = function( msg , title , btnLabel ){
					$timeout( function(){
						$scope.msg = msg || def.msg ;
						$scope.title = title || def.title ;
						$scope.btnLabel = btnLabel || def.btnLabel ; 
						$( $element ).modal('show') ;
					});
					
				}
			}
		}
	});

	//confirm 组件
	app.directive('ngModalConfirm' , function(){
		return {
			restrict 	: 	'E' ,
			replace 	: 	true ,
			scope 		: 	true ,
			templateUrl : 	'assets/modules/ng-modal-confirm/template.html' ,
			controller 	: 	function( $scope , $timeout , $element ){
				//angularjs 手动赋值需要调用$scope.$apply方法
				var def = {
					title 	: 	'标题' ,
					msg 	: 	'是否确认?' ,
					closeBtn: 	'取消' ,
					positiveBtn: '确定' 
				}

				//覆盖windows alert
				window.confirm = function( msg , positiveEvent , title , positiveBtn , closeBtn , closeEvent ){
					$timeout( function(){
						$scope.msg = msg || def.msg ;
						$scope.title = title || def.title ;
						$scope.positiveBtn = positiveBtn || def.positiveBtn ; 
						$scope.closeBtn = closeBtn || def.closeBtn ; 

						if( typeof positiveEvent == 'function' ){
							$($element).delegate('[name=positive-btn]' , 'click' , function(){
								$( $element ).modal('hide') ;
								positiveEvent() ;
							});
						}

						$( $element ).modal('show') ;
					});
				}
			}
		}
	});
	
	//开始配置应用
	app.config( function( $stateProvider , $urlRouterProvider , 
		$controllerProvider , $compileProvider , $provide , $filterProvider , $locationProvider ) {

		$compileProvider.aHrefSanitizationWhitelist('.*') ;
		$compileProvider.onChangesTtl(2) ;
		$compileProvider.imgSrcSanitizationWhitelist('.*') ;
		app.register = {
			'controller' 	:  	$controllerProvider.register ,
			'service' 		:	$provide.service ,
			'factory' 		: 	$provide.register ,
			'directive' 	: 	$compileProvider.directive ,
			'filter' 		: 	$filterProvider.register	
		};

		$locationProvider.hashPrefix('');

		$urlRouterProvider.when('' , '/') ;

		var stateProvider ;
		for( var key in urlConfig ){
			var view =  urlConfig[key] ;
			var state = {
				url 		: 	key  ,
				views 		: 	{
					'main' 		: 	{
						templateUrl 	: 	view['templateUrl'] && view['templateUrl'] + '?' + new Date().getTime() ,
						template 		: 	view['template'],
						controller 		: 	view['controller']
					}
				}
			};
			console.log('页面路由：' , state ) ;
			if( view['controller'] ){
				!function( control , jspath ){
					state['resolve'] ={
						load 	: 	function( $q ){
							var defer = $q.defer() ;
							require([ jspath ] , function( controller ){
								app.register.controller( control  , controller ) ;
								defer.resolve() ;
							});
							return defer.promise ;
						}
					}
				}( view['controller'] , view['controller'] ) ;
				
			}
			$stateProvider.state( view['key'] , state );
		}
	});
	return urlConfig ;
});